package com.bds.tjj.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bds.tjj.model.YearAreaData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.bds.tjj.model.YearData;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.YearSumService;
import com.bds.tjj.utils.Const;
import com.bds.tjj.utils.HttpUrlUtils;

/**
 * Created by guoyu on 2018/1/29.
 */
@RestController
public class YearSumController extends BaseAction {
    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(YearSumController.class);

    @Autowired
    YearSumService yearSumService;

    @RequestMapping(value = "/service/data/get_year_num/**", method = RequestMethod.GET)
    public Result getMonthNum(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
        Result ret = new Result();
        String type = HttpUrlUtils.extractPathFromPattern(httpRequest);

        YearData yearData = null;
        try {
            yearData = yearSumService.getYearNum(type);
            ret.setResult(Const.SUC, yearData);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, yearData);
            LOGGER.warn("查询year_num异常：", e);
        }

        return ret;
    }

    @RequestMapping(value = "/service/data/get_year_sum/**", method = RequestMethod.GET)
    public Result getYearSum(HttpServletRequest httpRequest, HttpServletResponse httpResponse) throws IOException {
        Result ret = new Result();
        String type = HttpUrlUtils.extractPathFromPattern(httpRequest);

        return ret;
    }

    /**
     * 例子：/service/data/get_year_trend/index1/2010-2017
     */
    @RequestMapping(value = "/service/data/get_year_trend/{prop}/{time}", method = RequestMethod.GET)
    public Result getYearTrend(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                               @PathVariable String prop, @PathVariable String time) throws IOException {
        Result ret = new Result();

        int startTime, endTime;
        try {
            startTime = Integer.parseInt(time.split("-")[0].substring(0, 4));
            endTime = Integer.parseInt(time.split("-")[1].substring(0, 4));
        } catch (Exception e) {
            ret.setResult(Const.FAIL, "参数异常");
            LOGGER.warn("get_year_trend 参数异常：", e);
            return ret;
        }

        try {
            List<YearData> list = yearSumService.getYearTrend(prop, startTime, endTime);
            ret.setResult(Const.SUC, list);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, null);
            LOGGER.warn("查询get_year_trend异常：", e);
        }

        return ret;
    }

    /**
     * 例子：/service/data/get_year_trend/index1/2010-2017
     */
    @RequestMapping(value = "/service/data/get_year_area_trend/{prop}/{time}/{area_id}", method = RequestMethod.GET)
    public Result getYearAreaTrend(HttpServletRequest httpRequest, HttpServletResponse httpResponse,
                                   @PathVariable String prop, @PathVariable String time,
                                   @PathVariable String area_id) throws IOException {
        Result ret = new Result();

        int startTime, endTime;
        try {
            startTime = Integer.parseInt(time.split("-")[0].substring(0, 4));
            endTime = Integer.parseInt(time.split("-")[1].substring(0, 4));
        } catch (Exception e) {
            ret.setResult(Const.FAIL, "参数异常");
            LOGGER.warn("get_year_area_trend 参数异常：", e);
            return ret;
        }

        try {
            List<YearAreaData> list = yearSumService.getYearAreaTrend(prop, startTime, endTime, area_id);
            ret.setResult(Const.SUC, list);
        } catch (Exception e) {
            ret.setResult(Const.FAIL, null);
            LOGGER.warn("查询get_year_area_trend异常：", e);
        }

        return ret;
    }
}
